﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bibilioteca.Dominio;
using Bibilioteca.Models;

namespace Bibilioteca.Controllers
{
    public class CatalogosController : Controller
    {
        NegocioCatalogos negocioCatalogos = new NegocioCatalogos();
        NegocioLogin negocioLogin = new NegocioLogin();

        //
        // GET: /Catalogos/

        public ActionResult Index()
        {
            if (!negocioLogin.permisosAdmin(Session))
            {
                String[] s = negocioLogin.redireccion(Session);
                return RedirectToAction(s[0], s[1]);
            }

            return View(negocioCatalogos.todosLosCatalogos());
        }

        //
        // GET: /Catalogos/Details/5

        public ActionResult Details(int id)
        {
            if (!negocioLogin.permisosAdmin(Session))
            {
                String[] s = negocioLogin.redireccion(Session);
                return RedirectToAction(s[0], s[1]);
            }

            return View(negocioCatalogos.GetCatalogoNumero(id.ToString()));
        }

        //
        // GET: /Catalogos/Create

        public ActionResult Create()
        {
            if (!negocioLogin.permisosAdmin(Session))
            {
                String[] s = negocioLogin.redireccion(Session);
                return RedirectToAction(s[0], s[1]);
            }

            return View();
        }

        //
        // POST: /Catalogos/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (!negocioLogin.permisosAdmin(Session))
            {
                String[] s = negocioLogin.redireccion(Session);
                return RedirectToAction(s[0], s[1]);
            }

            try
            {
                if (ModelState.IsValid)
                {
                    Catalogo s = new Catalogo(
                            -1,
                            collection["Nombre"]);

                    negocioCatalogos.insertarCatalogo(s);

                    return this.RedirectToAction("Index");
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Catalogos/Edit/5

        public ActionResult Edit(int id)
        {
            if (!negocioLogin.permisosAdmin(Session))
            {
                String[] s = negocioLogin.redireccion(Session);
                return RedirectToAction(s[0], s[1]);
            }

            return View(negocioCatalogos.GetCatalogoNumero(id.ToString()));
        }

        //
        // POST: /Catalogos/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (!negocioLogin.permisosAdmin(Session))
            {
                String[] s = negocioLogin.redireccion(Session);
                return RedirectToAction(s[0], s[1]);
            }

            try
            {
                if (ModelState.IsValid)
                {
                    Catalogo s = new Catalogo(
                            Convert.ToInt32(collection["Id"]),
                            collection["Nombre"]
                            );

                    negocioCatalogos.modificarCatalogo(s);

                    return this.RedirectToAction("Index");
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Catalogos/Delete/5

        public ActionResult Delete(int id)
        {
            if (!negocioLogin.permisosAdmin(Session))
            {
                String[] s = negocioLogin.redireccion(Session);
                return RedirectToAction(s[0], s[1]);
            }

            return View(negocioCatalogos.GetCatalogoNumero(id.ToString()));
        }

        //
        // POST: /Catalogos/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            if (!negocioLogin.permisosAdmin(Session))
            {
                String[] s = negocioLogin.redireccion(Session);
                return RedirectToAction(s[0], s[1]);
            }

            try
            {
                if (ModelState.IsValid)
                {
                    negocioCatalogos.eliminarCatalogo(id);

                    return this.RedirectToAction("Index");
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
